package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.result.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: TODO
 * @Author: 伍家钰
 * @Date: 2020/7/10 21:12
 * @Version: 1.0
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleServiceImpl;


    /**
     * 删除操作
     *
     * @param id:
     * @return :
     */
    @RequestMapping("/delete")
    public String delete(Long id) {
        // 完善后面可以做个判断
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";
    }

    /**
     * 去新增或者修改页面
     *
     * @param id:
     * @param model:
     * @return :
     */
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("role", roleServiceImpl.selectAll());
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        if (id != null) {
            Permission permission = permissionService.selectById(id);
            model.addAttribute("permission", permission);
        }
        return "permission/input";
    }

    /**
     * 新增或者修改
     *
     * @param permission:前端出入的permission对象
     * @param ids:用户选择的权限的id
     * @return :
     */
    @RequestMapping("/saveOrUpdate")
    public String update(Permission permission, Long[] ids) {
        if (permission.getId() != null) {
            permissionService.update(permission, ids);
        } else {
            permissionService.save(permission, ids);
        }
        //让浏览器重新发送一次请求
        return "redirect:/permission/list.do";
    }

    /**
     * 查询操作
     *
     * @param model:
     * @param qo:
     * @return :
     */
    @RequestMapping("/list")
    public String query(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }

    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        try {
            permissionService.reload();
            return new JsonResult(true, "加载成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "添加失败,请联系管理员!");
        }
    }
}
